package com.ruoyi.yard.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.yard.domain.CompWarehouseInfo;
import com.ruoyi.yard.domain.DeliveryPlanInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yard.domain.WaybillInfo;
import com.ruoyi.yard.service.IWaybillInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发运单Controller
 * 
 * @author ruoyi
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/yard/WaybillInfo")
public class WaybillInfoController extends BaseController
{
    @Autowired
    private IWaybillInfoService waybillInfoService;


    /**
     * 查询发运单列表
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaybillInfo waybillInfo)
    {
        startPage();
        List<WaybillInfo> list = waybillInfoService.selectWaybillInfoList(waybillInfo);
        return getDataTable(list);
    }

    /**
     * 查询发运单列表
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:homePageList')")
        @GetMapping("/homePage/list")
    public TableDataInfo homePage(WaybillInfo waybillInfo)
    {
        startPage();
        List<WaybillInfo> list = waybillInfoService.selectHomePageWaybillInfoList(waybillInfo);

        return getDataTable(list);
    }

    /**
     * 查询发货计划列表-统计
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics(WaybillInfo waybillInfo)
    {

        List<WaybillInfo> list = waybillInfoService.selectHomePageWaybillInfoList(waybillInfo);
        Long count=0L;
        Double weight=0D;
        for (WaybillInfo c:list){
            for (CompWarehouseInfo compWarehouseInfo : c.getCompWarehouseInfoList()) {
                count=count+compWarehouseInfo.getNeedCount();
                weight=weight+compWarehouseInfo.getNetWeight()*compWarehouseInfo.getNeedCount();
            }
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("count",count);
        ajaxResult.put("weight",weight);
        return ajaxResult;
    }

    /**
     * 导出发运单列表
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:export')")
    @Log(title = "发运单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaybillInfo waybillInfo)
    {
        List<WaybillInfo> list = waybillInfoService.selectWaybillInfoList(waybillInfo);
        ExcelUtil<WaybillInfo> util = new ExcelUtil<WaybillInfo>(WaybillInfo.class);
        util.exportExcel(response, list, "发运单数据");
    }

    /**
     * 获取发运单详细信息
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(waybillInfoService.selectWaybillInfoById(id));
    }

    /**
     * 新增发运单
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:add')")
    @Log(title = "发运单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaybillInfo waybillInfo)
    {
        return toAjax(waybillInfoService.insertWaybillInfo(waybillInfo));
    }

    /**
     * 修改发运单
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:edit')")
    @Log(title = "发运单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaybillInfo waybillInfo)
    {
        return toAjax(waybillInfoService.updateWaybillInfo(waybillInfo));
    }

    /**
     * 删除发运单
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:remove')")
    @Log(title = "发运单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(waybillInfoService.deleteWaybillInfoByIds(ids));
    }
    /**
     * 审核发运单
     */
        @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:toExamine')")
    @Log(title = "审核发运单", businessType = BusinessType.UPDATE)
    @PostMapping("/toExamine")
    public AjaxResult toExamine(@RequestBody JSONObject toExamine)
    {
        return toAjax(waybillInfoService.toExamine(toExamine));
    }

    /**
     * 打印发运单
     */
    @PreAuthorize("@ss.hasPermi('yard:WaybillInfo:printWaybill')")
    @Log(title = "打印发运单", businessType = BusinessType.UPDATE)
    @PostMapping("/printWaybill")
    public AjaxResult printWaybill(@RequestBody JSONObject params)
    {
        return toAjax(waybillInfoService.printWaybill(params));
    }


}
